package com.fstec.ks.master.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.fstec.ks.master.dao.ExamRoundDao;
import com.fstec.ks.master.dao.ExamServerDao;
import com.fstec.ks.master.dao.RoomRoundDao;
import com.fstec.ks.master.dao.ServerRoundDao;
import com.fstec.ks.master.dao.cond.RoomRoundCond;
import com.fstec.ks.master.dao.cond.ServerRoundCond;
import com.fstec.ks.master.service.ExamDataService;
import com.fstec.ks.master.vo.ExamRoundVO;
import com.fstec.ks.master.vo.ExamServerVO;
import com.fstec.ks.master.vo.RoomRoundVO;
import com.fstec.ks.master.vo.ServerRoundVO;

public class DefaultExamDataService implements ExamDataService {
	@Autowired
	private ExamRoundDao examRoundDao;
	@Autowired
	private ExamServerDao examServerDao;
	@Autowired
	private ServerRoundDao serverRoundDao;
	@Autowired
	private RoomRoundDao roomRoundDao;

	@Override
	public List<ExamRoundVO> getAllExamRounds() {
		return examRoundDao.selects();
	}

	@Override
	public List<ExamServerVO> getAllExamServers() {
		return this.examServerDao.selects();
	}

	@Override
	public List<ServerRoundVO> findServerRounds(int pageIdx, int pageSize, ServerRoundCond cond) {
		return this.serverRoundDao.selects(pageIdx, pageSize, cond);
	}

	@Override
	public List<RoomRoundVO> findRoomServerRounds(int pageIdx, int pageSize, RoomRoundCond cond) {
		return this.roomRoundDao.selects(pageIdx, pageSize, cond);
	}

	@Override
	public int saveExamServer(ExamServerVO examServer) {
		return this.examServerDao.update(examServer);
	}

	@Override
	public void addExamServer(ExamServerVO examServer, List<ExamRoundVO> roundVos, List<ServerRoundVO> serverRounds, List<RoomRoundVO> roomRounds) {
		this.examServerDao.insert(examServer);
		int i;
		int size;
		if(roundVos!=null && !roundVos.isEmpty()){
			size=roundVos.size();
			for(i=0;i<size;i++){
				this.examRoundDao.insert(roundVos.get(i));
			}
		}
		if(serverRounds!=null && !serverRounds.isEmpty()){
			size=serverRounds.size();
			ServerRoundVO serverRound;
			for(i=0;i<size;i++){
				serverRound=serverRounds.get(i);
				serverRound.setExamServerId(examServer.getId());
				this.serverRoundDao.insert(serverRound);
			}
		}
		if(roomRounds!=null && !roomRounds.isEmpty()){
			size=roomRounds.size();
			RoomRoundVO roomRound;
			for(i=0;i<size;i++){
				roomRound=roomRounds.get(i);
				roomRound.setExamServerId(examServer.getId());
				this.roomRoundDao.insert(roomRound);
			}
		}
	}

	@Override
	public int saveServerRound(ServerRoundVO serverRound) {
		return this.serverRoundDao.update(serverRound);
	}

	@Override
	public int saveRoomRound(RoomRoundVO roomRound) {
		return this.roomRoundDao.update(roomRound);
	}

	@Override
	public int removeExamServer(String serverId) {
		if(serverId==null) return 0;
		this.roomRoundDao.deleteServerRound(serverId);
		this.serverRoundDao.deleteServerRound(serverId);
		return this.examServerDao.delete(serverId);
	}
}
